package user

import (
	"encoding/json"

	"gitee.com/go-kade/go12/vblog/common"
	"golang.org/x/crypto/bcrypt"
)

type User struct {
	*common.Meta
	*CreateUserRequest
}

func NewUser(req *CreateUserRequest) *User {
	req.PasswordHash()
	return &User{
		Meta:              common.NewMeta(),
		CreateUserRequest: req,
	}
}

func (u *User) TableName() string {
	return "users"
}

func (u *User) String() string {
	dj, _ := json.Marshal(u)
	return string(dj)
}

func (u *User) CheckPassword(password string) error {
	// fmt.Println("password", password)
	// fmt.Println("password", string(password))
	// fmt.Println("u.Password", string(u.Password))
	return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
}
